//
//  MineViewController.m
//  YJlandlord
//
//  Created by 文彬 on 2018/6/24.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "MineViewController.h"
#import "MessItemTableViewCell.h"
#import "MineHeaderInfoView.h"
#import "UserInfoEidtViewController.h"
#import "SettingViewController.h"
#import "FeedBackViewController.h"
#import "WebViewViewController.h"
#import "WBScrollSelectView.h"
#import "ItemSelectTableViewCell.h"
#import "GetInvoiceViewController.h"
#import "CouponViewController.h"
#import "CollectViewController.h"
#import "CommentViewController.h"

@interface MineViewController ()<UITableViewDelegate,
    UITableViewDataSource,
    MineHeaderInfoViewDelegate>

@property (strong, nonatomic) UITableView *listTableView;

@property (strong, nonatomic) NSArray *itemDatas;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initPageControl];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark 页面相关
- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark 按钮点击
- (void)buttonClickHandle:(NSInteger)index{
    switch (index) {
        case 0: //意见反馈
            {
                FeedBackViewController *feedbackController = [[FeedBackViewController alloc]init];
                feedbackController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:feedbackController animated:YES];
            }
            break;
        case 1: //优惠券
        {
            CouponViewController *couponController = [[CouponViewController alloc]init];
            couponController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:couponController animated:YES];
        }
            break;
        case 2: //关注
        {
            CollectViewController *collectController = [[CollectViewController alloc]init];
            collectController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:collectController animated:YES];
        }
            break;
        case 4: //评论
        {
            CommentViewController *commentController = [[CommentViewController alloc]init];
            commentController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:commentController animated:YES];
        }
            break;
        case 5: //开发票
        {
            GetInvoiceViewController *getInvoiceController = [[GetInvoiceViewController alloc]init];
            getInvoiceController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:getInvoiceController animated:YES];
        }
            break;
     
            
        default:
            break;
    }
}

#pragma mark MineHeaderInfoViewDelegate
- (void)settingClicked{
    SettingViewController *settingControlelr = [[SettingViewController alloc]init];
    settingControlelr.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:settingControlelr animated:YES];
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==0?0.1:10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 220;
    }
    else if(indexPath.section==1){
        return 180;
    }
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    if(indexPath.section==1){
        
        ItemSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemSelectTableViewCell" forIndexPath:indexPath];
        [cell setFont:[UIFont systemFontOfSize:14]];
        [cell setData:self.itemDatas oneLineCount:4];
        WeakObj(self)
        cell.itemClicked = ^(id param) {
            [selfWeak buttonClickHandle:[param integerValue]];
        };
        return cell;
    }
    
    static NSString *iden = @"celliden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (indexPath.section==0) {
        
        MineHeaderInfoView *infoView = [[MineHeaderInfoView alloc]init];
        infoView.delegate  = self;
        [cell.contentView addSubview:infoView];
        [infoView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
        }];
    }
    else if(indexPath.section==2){
        UIImageView *imgView = [[UIImageView alloc]init];
        [imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.clipsToBounds = YES;
        [cell.contentView addSubview:imgView];
        [imgView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
        }];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorColor = kLineColor;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;        [_listTableView registerNib:[UINib nibWithNibName:@"ItemSelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"ItemSelectTableViewCell"];
        
        UIView *footView = [[UIView alloc]init];
        footView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 90);
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:@"我想成为房东" forState:UIControlStateNormal];
        [button setTitleColor:kTitleColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:18];
        [footView addSubview:button];
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@30);
            make.width.equalTo(@(SCREEN_WIDTH-60));
            make.height.equalTo(@40);
            make.centerY.equalTo(footView.centerY);
        }];
        [button buttonClick:^{
           
            NSURL *url = [NSURL URLWithString:@"YJ_Landlord://"];
            [[UIApplication sharedApplication] openURL:url];

        }];
        
        _listTableView.tableFooterView = footView;
    }
    
    return _listTableView;
}
-(NSArray*)itemDatas{
    if (_itemDatas==nil) {
        _itemDatas  = @[@{@"title":@"反馈",@"pic":@"header_default"},
                     @{@"title":@"优惠券",@"pic":@"header_default"},
                     @{@"title":@"关注",@"pic":@"header_default"},
                     @{@"title":@"邀请好友",@"pic":@"header_default"},
                     @{@"title":@"评价",@"pic":@"header_default"},
                     @{@"title":@"开发票",@"pic":@"header_default"},
                     @{@"title":@"在线客服",@"pic":@"header_default"},
                     @{@"title":@"关于意居",@"pic":@"header_default"}];
    }
    return _itemDatas;
}



@end
